//
//  MineAddressChangeViewController.m
//  MJApp
//
//  Created by wangliang on 15/3/29.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineAddressChangeViewController.h"
#import "MineAddressChangeTableViewCell.h"
#import "MineAddressDefaultTableViewCell.h"
#import "MineAddressDeleteTableViewCell.h"
#import "MineAddressAddViewController.h"

@interface MineAddressChangeViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) NSDictionary *infoDic;
@property (weak, nonatomic) IBOutlet UITableView *changeTableView;
@property (nonatomic,strong) NSArray *tableArray;
@property (nonatomic,strong) NSMutableDictionary *tableDic;


@end

@implementation MineAddressChangeViewController

- (instancetype)initWithInfoDic:(NSDictionary *)infoDic {
    
    if (self = [super init]) {
        
        self.infoDic = infoDic;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self Data];
    [self UI];
    [self NaviUI];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(change:)
                                                 name:@"addrInfo"
                                               object:nil];
}


- (void)change:(NSNotification *)notice {
    
    self.infoDic = notice.userInfo;
    [self Data];
    [self.changeTableView reloadData];
}

- (void)Data {
    
    self.tableArray = @[@"收货人",@"手机号码",@"邮政编码",@"所在地区",@"详情地址",@"删除收货地址"];
    NSString *area = [NSString stringWithFormat:@"%@ %@ %@",self.infoDic[@"economize"],self.infoDic[@"city"],self.infoDic[@"area"]];
    self.tableDic = [@{
    self.tableArray[0]:self.infoDic[@"name"],
    self.tableArray[1]:self.infoDic[@"phone"],
    self.tableArray[2]:self.infoDic[@"post_code"],
    self.tableArray[3]:area,
    self.tableArray[4]:self.infoDic[@"address"]
    } mutableCopy];
}

- (void)UI {
    
    self.title = @"地址详情";
    self.changeTableView.delegate = self;
    self.changeTableView.dataSource = self;
    self.changeTableView.backgroundColor = Color_Back_Black;
    self.changeTableView.separatorColor = [UIColor clearColor];
    self.changeTableView.contentInset = UIEdgeInsetsMake(0, 0, -44, 0);
}

- (void)NaviUI {
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [btn setTitle:@"修改" forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [btn addTarget:self action:@selector(changeButton:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)changeButton:(UIButton *)sender {
    
    [self.navigationController pushViewController:[[MineAddressAddViewController alloc] initWithDic:self.infoDic] animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return self.tableArray.count;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        MineAddressChangeTableViewCell *cell = (MineAddressChangeTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        return cell.cellHigh;
    }else {
        
        MineAddressDefaultTableViewCell *cell = (MineAddressDefaultTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        return cell.cellHigh;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        if(indexPath.row == self.tableArray.count - 1) {
            
            MineAddressDeleteTableViewCell *cell = [MineAddressDeleteTableViewCell initWithTableView:tableView];
            return cell;
        }else {
            
            MineAddressChangeTableViewCell *cell = [MineAddressChangeTableViewCell initWithTableView:tableView];
            cell.infoDic = @{
            @"name":self.tableArray[indexPath.row],
            @"desc":self.tableDic[self.tableArray[indexPath.row]]
            };
            return cell;
        }
        
    }else {
        
        MineAddressDefaultTableViewCell *cell = [MineAddressDefaultTableViewCell initWithTableView:tableView];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0 && indexPath.row == self.tableArray.count - 1) {
        
        [self delete];
    }else if (indexPath.section == 1) {
        
        NSIndexSet *ind = [NSIndexSet indexSetWithIndex:indexPath.section];
        [tableView reloadSections:ind withRowAnimation:UITableViewRowAnimationNone];
    }
}

- (void)delete {
    
    NSDictionary *dic = @{@"address_id":self.infoDic[@"id"]};
    [WLHttpRequestAfter userdelAddressWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        
        if ([resultDictionary[@"status"] integerValue] == 1) {
            
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"成功删除地址"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"addrChange" object:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:resultDictionary[@"msg"]];
        }
        
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"addrInfo" object:nil];
}


@end
